<?php
class files extends CI_Model{

	function getAllFile($keyword='',$offset='',$paging=''){
		$data = array();
		$sql = "  SELECT * ";
		$sql .= " FROM tbl_obat ";
		$sql .= " WHERE  flag_opname = 0 ";
		
		/*if($this->session->userdata('user_group')!= 1):
			$sql .= " AND u.username = '".$this->session->userdata('user_name')."'";
		endif;*/
		
		/*$sql .= " AND folder_id = 0 ";
		if($keyword<>''){
			$sql .= " AND u.file_name LIKE '%".$keyword."%'";
		}*/
		
		$sql .= " ORDER BY ABS(id_obat) ASC ";
		if($paging<>''){
			$sql.=	" LIMIT ";
			 if($offset<>''){
				$sql.=	$offset.",";
			}else{
				$sql.=	"0,";
			}
			$sql.=	$paging;
		}
		$Q = $this->db->query($sql);
		if ($Q->num_rows() > 0):
			foreach ($Q->result_array() as $row):
				$data[] = $row;
			endforeach;
		endif;
		$Q->free_result();
		return $data;
	}
	
	function getAllFolder($keyword='',$id='',$offset='',$paging=''){
		$data = array();
		$sql = "  SELECT * ";
		$sql .= " FROM userfile u ";
		$sql .= " WHERE u.username = '".$this->session->userdata('user_name')."'";
		$sql .= " AND folder_id <> 0 ";
		$sql .= " AND folder_id =".$id;
		if($keyword<>''){
			$sql .= " AND u.file_name LIKE '%".$keyword."%'";
		}
		
		$sql .= " ORDER BY u.created_date DESC ";
		if($paging<>''){
			$sql.=	" LIMIT ";
			 if($offset<>''){
				$sql.=	$offset.",";
			}else{
				$sql.=	"0,";
			}
			$sql.=	$paging;
		}
		$Q = $this->db->query($sql);
		if ($Q->num_rows() > 0):
			foreach ($Q->result_array() as $row):
				$data[] = $row;
			endforeach;
		endif;
		$Q->free_result();
		return $data;
	}
	
	function getDataFile($id){
		$data = array();
		$sql = "  SELECT id_obat,box_sedang,box_kecil,nm_obat,sisa ";
		$sql .= " FROM vw_stok ";
		$sql .= " WHERE id_obat = ".$id." LIMIT 1";
		$Q = $this->db->query($sql);
		if ($Q->num_rows() > 0):
				$data =$Q->row_array();
		endif;
		return $data;
	}
	
	function getAllDir(){
		$data = array();
		$sql = "  SELECT * ";
		$sql .= " FROM userfile u ";
		$sql .= " WHERE u.username = '".$this->session->userdata('user_name')."'";
		$sql .= " AND file_type='application/folder' ";
		$sql .= " AND folder_id = 0 ";
		$sql .= " ORDER BY u.file_name ASC ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows() > 0):
			foreach ($Q->result_array() as $row):
				$data[] = $row;
			endforeach;
		endif;
		$Q->free_result();
		return $data;
	}
	
	function getAllParentDir(){
		$data = array();
		$sql  = " SELECT * ";
		$sql .= " FROM userfile u ";
		$sql .= " WHERE u.username = '".$this->session->userdata('user_name')."'";
		$sql .= " AND file_type='application/folder' ";
		$sql .= " AND folder_id <> 0 ";
		$sql .= " ORDER BY u.file_name ASC ";
		$Q = $this->db->query($sql);
		if ($Q->num_rows() > 0):
			foreach ($Q->result_array() as $row):
				$data[] = $row;
			endforeach;
		endif;
		$Q->free_result();
		return $data;
	}
	
	function getCheckFolder($folder){
		$data = array();
		$sql = "  SELECT * ";
		$sql .= " FROM userfile u ";
		$sql .= " WHERE u.username = '".$this->session->userdata('user_name')."'";
		$sql .= " AND u.file_name = '".$folder."'";
		$sql .= " AND u.folder_id = '".$this->input->post('id')."'";
		$Q = $this->db->query($sql);
		return $Q->num_rows();
	}
	
	function getSave($name,$type,$size){
		if($this->input->post('id')):
			$id = $this->input->post('id');
		else:
			$id	= '0';
		endif;
		
		$value = array( 
						'file_name'		=>	$name,
						'file_type'		=>	$type,
						'file_size'		=>	$size,
						'folder_id'		=>	$id,
						'username'		=>	$this->session->userdata('user_name'),
						'created_date'	=>	date('Y-m-d H:i:s')
					  );
		$this->db->insert('userfile',$value);			  
	}
	
	function getRemove($id){
		$this->db->where('file_id',$id);
		$this->db->where('username',$this->session->userdata('user_id'));
		$this->db->delete('userfile');
		
		$this->db->where('folder_id',$id);
		$this->db->where('username',$this->session->userdata('user_id'));
		$this->db->delete('userfile');
	}
	
}	